$(function(){
	
//	选择分类进行异步传输
	$('[name=category_cid]').change(function(){
		var tid = $('option:selected').attr('tid');
		$.ajax({
			type:"post",
			url:tpyePath,
			data:{tid:tid},
			dataType:'json',
			success:function(phpData){
//				规格(可选)
				var spec = '';
//				属性(不可选)
				var attr = '';
				$.each(phpData, function(k,v) {
//					属性
					if (v.class == 0) {
							attr += '<div class="control-group">';
							attr += '<label class="control-label" for="form-field-1">';
							attr += v.atname;
							attr += '</label><div class="controls"><select name=';
							attr += 'attr['+v.atid+'][value][]';
							attr +='>';
							$.each(v.atvalue, function(kk,vv) {
								attr += '<option value="'+vv+'">'+vv+'</option>';
							});
							attr +='</select><a style="margin-left:5px;" class="del btn btn-mini btn-danger" href="javascript:;"><i class="icon-minus"></i>删除属性</a></div></div>';
					} else{//规格
							spec += '<div class="control-group">';
							spec += '<label class="control-label" for="form-field-1">'+v.atname+'</label><div style="margin-bottom:5px;" class="controls">';
							spec += '<select name="';
//							添加name值
							spec += 'spec['+v.atid+'][value][]';
							spec += '">';
//							循环得到value值
							$.each(v.atvalue, function(kk,vv) {
								spec += '<option value="'+vv+'">'+vv+'</option>';
							});
							spec +='</select></td><td> &nbsp;&nbsp;附加价值：</td>';
							spec +='<td><input type="text" name="spec['+v.atid+'][price][]" id="" value="0" /></td><td><a href="javascript:;" class="add btn btn-small btn-success"><i class="icon-plus"></i>添加规格</a></td></tr></div></div>';
					}
				});
				$('.goods_attr').html(attr);
				$('.goods_spec').html(spec);
			}
		});
		
	})

	


//	添加一个规格
	$('.add').live('click',function(){
		var line = $(this).parents('.control-group');
		var clone_line = line.clone();
		clone_line.find('.add').removeClass('btn-success add').addClass('btn-danger del').html('<i class="icon-minus"></i>删除规格</a>');
		line.after(clone_line);
	})
	
	$('.del').live('click',function(){
		$(this).parents('.control-group').remove();
	})
	
	$("[name=category_cid]").live('change',function(){
		var val =  $('option:selected').attr('pid');
		if(val ==0) alert("顶级分类不能添加商品！");
	})
	
	
	
	
})